package cn.zb.user;

import com.jfinal.aop.Interceptor;
import com.jfinal.core.ActionInvocation;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class UserInterceptor implements Interceptor {

    public static final String ZB_ADMIN_USER = "zb_admin_user";
	
	public void intercept(ActionInvocation ai) {
        HttpServletRequest request = ai.getController().getRequest();
        HttpServletResponse response = ai.getController().getResponse();
        User user = (User) request.getSession().getAttribute(ZB_ADMIN_USER);
        if(user == null) {
            try {
                response.sendRedirect("/");
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            ai.invoke();
        }
	}
}
